package algotithm.leetcode.dfs.test200;

/**
 * @author zhouyanxiang
 * @Date 2021-01-2021/1/13-22:33
 */
public class Solution {

    private int result;
    private int n;
    private int m;
    public int numIslands(char[][] grid) {
        n = grid.length;
        m = grid[0].length;
        result = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == '1') {
                    result++;
                    dfs(grid,i,j);
                }
            }
        }
        return result;
    }

    public void dfs(char[][] grid,int i,int j) {
        if (i < 0 || j < 0 || i >= n || j >= m) {
            return;
        }
        if (grid[i][j] == '0') {
            return;
        }
        grid[i][j] = '0';
        dfs(grid,i-1,j);
        dfs(grid,i+1,j);
        dfs(grid,i,j-1);
        dfs(grid,i,j+1);
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        char[][]grid = {
                {'1','1','1','1','0'},
                {'1','1','0','0','0'},
                {'1','1','0','0','0'},
                {'0','0','0','0','0'}
        };
        int numIslands = solution.numIslands(grid);
        System.out.println(numIslands);
    }

}
